﻿using System;
using System.Globalization;
using System.IO;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Runtime.Serialization;
using System.Security.Permissions;
using Microsoft.Practices.EnterpriseLibrary.Caching;



namespace Sealight.CoreAPI.Caching
{
    /// <summary>
    ///	This class tracks a file cache dependency.
    /// </summary>
    [Serializable]
    [ComVisible(false)]
    public class DirectoryDependency : ICacheItemExpiration, IDisposable
    {
        private readonly string dependencyDirectory;


        private DateTime preModified = DateTime.Now;



        private Dictionary<string, DateTime> files = new Dictionary<string, DateTime>();

        /// <summary>
        /// Initializes a new instance of the <see cref="DirectoryDependency"/> class.
        /// </summary>
        /// <param name="fullDirectory">The full directory.</param>
        public DirectoryDependency(string fullDirectory)
        {
            if (string.IsNullOrEmpty(fullDirectory))
            {
                throw new ArgumentException("fullDirectory");
            }

            dependencyDirectory = Path.GetFullPath(fullDirectory);
            EnsureTargetFileAccessible();

            if (!Directory.Exists(dependencyDirectory))
            {
                throw new ArgumentException("ExceptionInvalidFileName", "fullFileName");
            }
            preModified = Directory.GetLastWriteTimeUtc(dependencyDirectory);
            //Initialize();
        }
        //private void Initialize()
        //{
        //    preModified = Directory.GetLastWriteTime(this.dependencyDirectory);

        //}
        /// <summary>
        /// Gets the name of the dependent file.
        /// </summary>
        /// <value>
        /// The name of the dependent file.
        /// </value>
        public string DirectoryName
        {
            get { return dependencyDirectory; }
        }

        /// <summary>
        ///	Specifies if the item has expired or not.
        /// </summary>
        /// <returns>Returns true if the item has expired, otherwise false.</returns>
        public bool HasExpired()
        {
            bool expired = false;

            if (!Directory.Exists(this.dependencyDirectory))
            {
                return true;
            }

            var lastestModified = Directory.GetLastWriteTime(this.dependencyDirectory);


            expired = preModified != lastestModified;

            preModified = lastestModified;

            return expired;
        }

        /// <summary>
        ///	Notifies that the item was recently used.
        /// </summary>
        public void Notify()
        {
        }

        /// <summary>
        /// Not used
        /// </summary>
        /// <param name="owningCacheItem">Not used</param>
        public void Initialize(CacheItem owningCacheItem)
        {
        }

        private void EnsureTargetFileAccessible()
        {
            // keep from changing during demand
            string file = dependencyDirectory;
            FileIOPermission permission = new FileIOPermission(FileIOPermissionAccess.Read, file);
            permission.Demand();
        }

        #region IDisposable Members

        public void Dispose()
        {
        }

        #endregion
    }
}

